package com.app.scale.video;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;


import androidx.annotation.NonNull;

import com.app.scale.R;
import com.app.scale.basis.BasisActivity;
import com.app.scale.util.ThreadUtils;
import com.hikvision.open.hikvideoplayer.HikVideoPlayer;
import com.hikvision.open.hikvideoplayer.HikVideoPlayerCallback;
import com.hikvision.open.hikvideoplayer.HikVideoPlayerFactory;

import butterknife.BindView;
import butterknife.OnClick;
import my.LogUtil;

/***
 * 关于我们
 * **/
public class HKVideoDetailActivity extends BasisActivity {


    @BindView(R.id.view_play)
    View view_play;
    @BindView(R.id.img_play_icon)
    ImageView img_play_icon;

    @BindView(R.id.textureview)
    TextureView textureView;

    boolean isPlay  = false;
    String url;

    HikVideoPlayer mPlayer;

    @Override
    public void initViews() {
        // TODO Auto-generated method stub
        super.initViews();
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.video_hk_detail_activity);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.initData(savedInstanceState);
        url= getIntent().getStringExtra("url");
        if(TextUtils.isEmpty(url)){
            showToast("视频地址不存在");
            finish();
            return;
        }
        LogUtil.i("海康----地址----》"+url);
        initHk();
        setPlayView();

//        startPlay();
    }

    void initHk(){
        //创建HikVideoPlayer实例
         mPlayer = HikVideoPlayerFactory.provideHikVideoPlayer();
        //设置播放画面显示surface,仅支持TextureView
//        TextureView textureView = findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                mPlayer.setSurfaceTexture(textureView.getSurfaceTexture());
                textureView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startPlay();
                    }
                },500);
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });

    }

    void startPlay(){
        ThreadUtils.doOnThread(new ThreadUtils.ThreadTask() {
            @Override
            public void doOnThread() {
                //此方法需要在子线程中执行
                mPlayer.startRealPlay(url, new HikVideoPlayerCallback(){
                    //播放结果回调
                    // status 共三种状态：SUCCESS（播放成功）、FAILED（播放失败）、EXCEPTION（取流异常）
                    // 错误码，只有 FAILED 和 EXCEPTION 才有值
                    // 注意：回调仍为子线程
                    @Override public void onPlayerStatus(@NonNull Status status, int errorCode) {
                        isPlay = true;
                        showToast("播放成功");
                        ThreadUtils.doOnUIThread(new ThreadUtils.UITask() {
                            @Override
                            public void doOnUI() {
                                setPlayView();
                            }
                        });
                    }
                });
            }
        });

    }

    void stopPlay(){
        mPlayer.stopPlay();
        isPlay = false;
        setPlayView();
    }

    @OnClick(R.id.view_play)
    void viewplay() {
        isPlay = !isPlay;
        if(isPlay){
            startPlay();
        }else{
            stopPlay();
        }
        setPlayView();
    }

    void setPlayView(){
        if(isPlay){
            view_play.setBackground(null);
            img_play_icon.setVisibility(View.GONE);

        }else{
            view_play.setBackgroundResource(R.color.video_pause_bg);
            img_play_icon.setVisibility(View.VISIBLE);
        }

    }

    @OnClick(R.id.btn_video)
    void btnvideo() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
